var t = getApp(),
  core = t.requirejs("core"),
  e = t.requirejs("check");
Page({
  data: {
    route: "product",
    menu: t.tabBar,
    menu_show: false,
    icons: t.requirejs("icons"),
    page: 1,
    loaded:false,
    list: [],
    cate_id: "",
    cate_id1: "",
    cate_index: "",
    cate_id2: "",
    cate_id3: "",
    category:[],
    cate2:[],
    cate3:[]
  },
  //底部导航跳转
  menu_url: function (k) {
    core.menu_url(k, 2);
  },
  onLoad: function (t) {

    if (t != null && t != undefined) {
      this.setData({
        tabbar_index: t.tabbar_index ? t.tabbar_index : -1
      });
    }

    core.setting();
    this.setData({
      menu: getApp().tabBar
    })
    
    if (this.data.tabbar_index >= 0) {
      this.setData({
        showtabbar: true,
      })
    }

    var cate_id = t.id || ''
    this.setData({
      cate_id: cate_id
    });  
    this.initCategory();
    this.getList();
  },
  onShow: function () {
    
  },
  /**
  * 下拉刷新
  */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      loaded: false,
      list: []
    })
    this.initCategory();
    this.getList();
    wx.stopPullDownRefresh();
  },
  /**
   *上拉加载
   */
  onReachBottom: function () {
    this.data.loaded || this.getList()
  },

  /**
   *获取一级分类
   * @return array
   */
  initCategory: function () {
    var t = this;
    core.get("goods/GetCate", {}, function (a) {
      t.setData({
        category: a.info,
      })
    })
  },

  /**
   *获取商品列表
   * @return array
   */
  getList: function () {
    var t = this;
      core.get("goods/GoodsList", {
        page:t.data.page,
        cate_id: t.data.cate_id
      }, function (a) {
        if (a.code == 0) {
          if (a.info.length == 0) {
            t.setData({
              loaded:true
            })
            return false;
          }
          var e = {};
          a.info.length > 0 && (e.page = t.data.page + 1, e.list = t.data.list.concat(a.info), a.info.length < 10 && (e.loaded = true)),
            t.setData(e)
        } else {
          core.alert(a.msg)
        }

      },true)

  },
  selected: function(t){
    var that=this,
    index = core.pdata(t).index,
    cate_id=core.pdata(t).type;
    that.setData({
      list: [],
      page: 1,
      loaded:false,
      cate_id : cate_id,
      scroll_left:index*20
    })
    that.getList()
  },
  cate_select: function (t) {
   
    var that = this,
      cate = that.data.category,
      data = core.pdata(t),
      e={};
    console.log(data)
    if (data.level== 1){
      e['cate_index'] = data.index;
      e['cate_id2']='';
      e['cate_id3'] = '';
      e.cate3=[];
      if(data.id==''){
        e.cate2 = [];
      }else{
        e.cate2 = cate[data.index].cate;
      }
     
    }
    if (data.level == 2) {
      e.cate3 = cate[that.data.cate_index]['cate'][data.index].cate;
    }
      e['cate_id']= data.id;
    e['cate_id' + data.level] = data.id;
    that.setData(e)
  },
  confirm: function () {
    this.setData({
      list: [],
      page: 1,
      loaded: false,
      isFilterShow: false,
    })
    this.getList()
  },
  showFilter: function () {
    this.setData({
      isFilterShow: !this.data.isFilterShow
    })
  },

  //分享
  onShareAppMessage: function () {
    return e.onShareAppMessage()
  }
  
  
})